$(function (){

	$("#warehouse_id,#supplier_ipt,#variety_ipt").on("focus",function (){
		$(this).val("");
	})

	$("#warehouse_id").on("change",function (){
		var warehouseId = $(this).val().replace(/-/g,'');
		console.log(warehouseId);
		$.get("/products/likeWarehouseId",{'warehouseId':warehouseId},function (data){
			$("#warehouse_id_select").empty();
			$("#warehouse_id_select").append("<option value='0'>请选择具体的入库订单</option>");
			$.each(data,function (i,item){
				$("#warehouse_id_select").append("<option value='"+item+"'>"+item+"</option>");
			})
		})
	})

	$("#submit_button").on("click",function (){
		var warehouseId = $("#warehouse_id_select").val();
		$.post("/products/warehouseIdAllList",{'warehouseId':warehouseId},function (data){
			tableToExcel(data);
		})
	})
})

//	配置打印信息
const tableToExcel = (jsonData) => {
	// 要导出的json数据
	// const jsonData = JSON.parse(listStr);
	console.log(JSON.stringify(jsonData));
	console.log(jsonData[0]['createTime'].substring(0,10));
	console.log(jsonData[0]['remake']);
	var date = new Date();

	var dateStr = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()
	var fontSize1 = 18;
	var fontSize2 = 16;


	var startStr = "<tr><td style=\"font-size: 27px; text-align: center\" colspan=\"8\">石家庄福澈研磨科技有限公司入库单</td></tr>\n" +
		"\t\t<tr >\n" +
		"\t\t\t<td colspan='2' style=\"text-align: center;font-size: "+fontSize1+"px; font-weight: bold;\">供货单位</td>\n" +
		"\t\t\t<td colspan='6' style='font-size: "+fontSize2+"px;'>"+jsonData[0]['supplier']+"</td>\n" +
		"\t\t</tr>\n" +
		"\t\t<tr>\n" +
		"\t\t\t<td colspan='2'  style=\"text-align: center;font-size:"+fontSize1+"px;font-weight: bold;\">联系人</td>\n" +
		"\t\t\t<td colspan='2' style='font-size: "+fontSize2+"px;'>"+jsonData[0]['contact']+"</td>\n" +
		"\t\t\t<td colspan=''  style=\"text-align: center;font-size: "+fontSize1+"px;font-weight: bold;\">电话</td>\n" +
		"\t\t\t<td colspan='' style='font-size: "+fontSize2+"px;'>"+jsonData[0]['telephone']+"</td>\n" +
		"\t\t\t<td colspan=''  style=\"text-align: center;font-size: "+fontSize1+"px;font-weight: bold;\">日期</td>\n" +
		"\t\t\t<td colspan='' style='font-size: "+fontSize2+"px;'>"+dateStr+"</td>\n" +
		"\t\t</tr>\n" +
		"\t\t<tr>\n" +
		"\t\t\t<th style='font-size: "+fontSize1+"px;'>序号</th>\n" +
		"\t\t\t<th style='font-size: "+fontSize1+"px;' >品牌</th>\n" +
		"\t\t\t<th style='font-size: "+fontSize1+"px;' >品名</th>\n" +
		"\t\t\t<th style='font-size: "+fontSize1+"px;' >型号</th>\n" +
		"\t\t\t<th style='font-size: "+fontSize1+"px;' >规格</th>\n" +
		"\t\t\t<th style='font-size: "+fontSize1+"px;' >单价(元)</th>\n" +
		"\t\t\t<th style='font-size: "+fontSize1+"px;' >数量</th>\n" +
		"\t\t\t<th style='font-size: "+fontSize1+"px;' >金额</th>\n" +
		"\t\t</tr>";

	var num = 0;

	// 列标题
	let str = startStr;
	// 循环遍历，每行加入tr标签，每个单元格加td标签
	for(let i = 0 ; i < jsonData.length ; i++ ){

		str+='<tr>';

		str+="\t\t\t<td style='font-size: "+fontSize2+"px;' >"+(i+1)+"</td>\n" +
			"\t\t\t<td style='text-align: center;font-size: "+fontSize2+"px;'>"+jsonData[i]['brandName']+"</td>\n" +
			"\t\t\t<td style='text-align: center;font-size: "+fontSize2+"px;'>"+jsonData[i]['varietyName']+"</td>\n" +
			"\t\t\t<td style='text-align: center;font-size: "+fontSize2+"px;'>"+jsonData[i]['typeName']+"</td>\n" +
			"\t\t\t<td style='text-align: center;font-size: "+fontSize2+"px;'>"+jsonData[i]['standard']+"</td>\n" +
			"\t\t\t<td style='text-align: center;font-size: "+fontSize2+"px;'>"+jsonData[i]['count']+"</td>\n" +
			"\t\t\t<td style='text-align: center;font-size: "+fontSize2+"px;'>"+jsonData[i]['price']+"</td>\n" +
			"\t\t\t<td style='text-align: center;font-size: "+fontSize2+"px;'>"+jsonData[i]['money']+"</td>\n";

		str+='</tr>';
		num+=parseFloat(jsonData[i]['money']);
	}




	var endStr = "<tr>\n" +
		"\t\t\t<td colspan='2'  style='text-align: center;font-size: "+fontSize1+"px;font-weight: bold;'>合计</td>\n" +
		"\t\t\t<td colspan='6'  style='font-size: "+fontSize2+"px;text-align: left'>"+num+"</td>\n" +
		"\t\t</tr>\n" +
		"\t\t<tr>\n" +
		"\t\t\t<td colspan='2' style='text-align: center;font-size: "+fontSize1+"px;font-weight: bold;'>备注</td>\n" +
		"\t\t\t<td colspan='6'style='font-size: "+fontSize2+"px;'>"+jsonData[0]['remake']+"</td>\n" +
		"\t\t</tr>\n" +
		"\t\t<tr>\n" +
		"\t\t\t<td colspan='2' style='text-align: center;font-size: "+fontSize1+"px;font-weight: bold;'>制单</td>\n" +
		"\t\t\t<td colspan='2' style='font-size: "+fontSize2+"px;'></td>\n" +
		"\t\t\t<td colspan='1' style='text-align: center;font-size: "+fontSize1+"px;font-weight: bold;'>审核</td>\n" +
		"\t\t\t<td colspan='3' style='font-size: "+fontSize2+"px;'></td>\n" +
		"\t\t</tr>";

	str+=endStr;
	// Worksheet名
	const worksheet = 'Sheet1'
	const uri = 'data:application/vnd.ms-excel;base64,';

	// 下载的表格模板数据
	var template = '<html xmlns:o="urn:schemas-microsoft-com:office:office"  xmlns:x="urn:schemas-microsoft-com:office:excel"';
	template +=' xmlns="http://www.w3.org/TR/REC-html40">';
	template +=' <head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>';
	template +=' <x:Name>'+worksheet+'</x:Name>';
	template +=' <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>';
	template +=' </x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->';
	template +=' </head><body><table border="1" width="700">'+str+'</table></body></html>';

	// 下载模板
	window.location.href = uri + base64(template);
};

// 输出base64编码
const base64 = s => window.btoa(unescape(encodeURIComponent(s)));


